import { ROUTES } from '@/services/api'
import { request, METHOD, removeAuthorization } from '@/utils/request'

/**
 * 获取加密KEY
 */
export async function getKey(phone) {
  return request("/api/systemUser/getKey", METHOD.POST, {
    phone: phone
  })
}
// 验证码
export async function getLoginCode(data) {
  return request("/api/systemUser/getLoginCode", METHOD.POST, data)
}
/**
 * 登录服务
 * {
  "code": "string",
  "loginType": 0,
  "password": "string",
  "phone": "string"
}
 * @param phone 账户名/手机号
 * @param password 账户密码
 * @param loginType 1:密码登录，2:验证码登录
 * @returns {Promise<AxiosResponse<T>>}
 */
export async function login(data) {
  return request("/api/systemUser/submit", METHOD.POST, data)
}

export async function getRoutesConfig() {
  return request(ROUTES, METHOD.GET)
}
/**
 * 获取用户列表
 */
export async function getSystemUserList(params) {
  return request("api/systemUser/getSystemUserList", METHOD.POST,
    params
  )
}
/**
 * 获取用户个人信息
 * @param {*} userId
 * @returns
 */
export async function getSystemUser(userId) {
  return request("api/systemUser/getSystemUser", METHOD.POST,
    { user_id: userId }
  )
}
/**
 * 重置密码
 * @param {*} params
 * @returns
 */
export async function resetPassword(params) {
  return request("api/systemUser/resetPassword", METHOD.POST,
    params
  )
}
/**
 * 添加用户
 * /systemUser/addSystemUser
 */
export async function addSystemUser(params) {
  return request("api/systemUser/addSystemUser", METHOD.POST,
    params
  )
}
/**
 * 添加角色信息
 * /systemUser/addRoleUserList
 */
export async function addRoleUserList(params) {
  return request("api/systemUser/addRoleUserList", METHOD.POST,
    params
  )
}
/**
 * 删除用户
 * /systemUser/deleteSystemUser
 */
export async function deleteSystemUser(userId) {
  return request("api/systemUser/deleteSystemUser", METHOD.POST,
    { user_id: userId}
  )
}
/**
 * 启用/禁用
 * /systemUser/enableDisable
 */
export async function enableDisable(userId, enableDisable) {
  return request("api/systemUser/enableDisable", METHOD.POST,
    { userId, enableDisable }
  )
}
/**
 * 退出登录
 */
export function logout() {
  localStorage.removeItem(process.env.VUE_APP_ROUTES_KEY)
  localStorage.removeItem(process.env.VUE_APP_PERMISSIONS_KEY)
  localStorage.removeItem(process.env.VUE_APP_ROLES_KEY)
  removeAuthorization()
}
export default {
  login,
  logout,
  getRoutesConfig,
  getLoginCode,
  getSystemUserList,
  getSystemUser,
  addSystemUser,
  resetPassword,
  addRoleUserList,
  deleteSystemUser,
  enableDisable
}
